package com.freeunion.zhibo.utils;

import android.os.Build;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;

import static com.tencent.qalsdk.service.QalService.context;

/**
 * Created by silas on 2016/12/22.
 */

public class FileUtils {
    /**
     * 将错误日志写入到 SD/Android/data/包名/file/crash.txt
     * @param throwable
     */
    public static void writeError2File(Throwable throwable) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        throwable.printStackTrace(pw);
        String str = sw.toString(); // 得到错误堆栈信息

        try {
            StringBuffer sb = new StringBuffer();
            File file = new File(context.getExternalFilesDir(null), "crash.txt");
            FileOutputStream fos = new FileOutputStream(file, true);

            // Build有手机设备的各种信息，得到所有字段
            sb.append("时间:"+new Date(System.currentTimeMillis()).toString()+"\n");
            sb.append("系统定制商："+ Build.BRAND);
            sb.append("\n设置参数："+Build.DEVICE);
            sb.append("\n显示屏参数："+Build.DISPLAY);
            sb.append("\n硬件名称："+Build.HARDWARE);
            sb.append("\n硬件制造商："+Build.MANUFACTURER);
            sb.append("\n版本："+Build.MODEL);
            sb.append("\n硬件序列号："+Build.SERIAL);
            sb.append("\n手机制造商："+Build.PRODUCT);

            sb.append("\n错误信息："+str);
            fos.write(sb.toString().getBytes());
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
